package com.fw.application.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.application.controller.base.UserController;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.TbSysRule;
import com.fw.system.web.model.entity.TbUser;
import com.fw.system.web.model.entity.TbUserCityJoin;
import com.fw.system.web.service.ITbSysRuleService;
import com.fw.system.web.service.ITbUserCityJoinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;

import static com.fw.mes.ResultUtils.success;

/**
 * <p>
 * 规则配置表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/web/rule")
@CrossOrigin
@Api(tags = "系统规则相关")
public class TbSysRuleController implements UserController {


    @Autowired
    private ITbSysRuleService ruleService;
    @Autowired
    private ITbUserCityJoinService userCityJoinService;


    @GetMapping("/checkStart")
    @ApiOperation("检查活动是否开始 如果返回值是200放行否则拦住")
    public Result checkStart() {

        TbSysRule startAndEndTime = ruleService.getById(8);
        if (LocalDateTime.now().compareTo(startAndEndTime.getStartTime()) < 0)
            return new Result<>().fail(398, "活动还未开始哦");
        return success();
    }

    @GetMapping("/queryMoveAbout")
    @ApiOperation("查询活动开关，返回值：当前用户信息不为空可以进去页面，")
    public Result queryMoveAbout() {
        TbUser user = this.getUser();
        int size = userCityJoinService.list(Wrappers.<TbUserCityJoin>lambdaQuery()
                .eq(TbUserCityJoin::getUserId, user.getId()).eq(TbUserCityJoin::getIsType, 2)).size();
        TbSysRule startAndEndTime = ruleService.getById(8);
        if (LocalDateTime.now().compareTo(startAndEndTime.getEndTime()) > 0) {
            if (size != 0) {
                return success(user);
            }
            return new Result<>().fail(398, "活动已经截止了");
        }
        return success(user);
    }

    @GetMapping("/queryAbout")
    @ApiOperation("查询是否加入战队")
    public Result queryAbout() {
        TbUser user = this.getUser();
        TbUserCityJoin one = userCityJoinService.getOne(Wrappers.<TbUserCityJoin>lambdaQuery()
                .eq(TbUserCityJoin::getUserId, user.getId()));
        if (one == null)
            return new Result().fail(600, "您还未加入战队，请去选择哦！");
        return success();
    }

    @GetMapping("/queryMoveAboutTime")
    @ApiOperation("查询活动开始时间和结束时间")
    public Result queryMoveAboutTime() {

        TbSysRule byId = ruleService.getById(8);
        return success(byId);
    }

    @ApiOperation("兑换炸弹护盾所需积分，炸掉的步数，炸弹使用上限")
    @GetMapping("/getRuleByBomb")
    public Result getRuleByBomb() {

        String bomb = ruleService.getById(2).getRuleValue();
        String shield = ruleService.getById(3).getRuleValue();
        //炸掉的步数
        String bombCount = ruleService.getById(12).getRuleValue();
        //炸弹使用上限
        String useBombCount = ruleService.getById(5).getRuleValue();

        Integer integral = this.getUser().getIntegral();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("bomb", bomb);
        hashMap.put("shield", shield);
        hashMap.put("bombCount", bombCount);
        hashMap.put("useBombCount", useBombCount);
        hashMap.put("integral", integral.toString());
        return success(hashMap);
    }

    @GetMapping("/getIndexImg")
    @ApiOperation("查询首页活动配图")
    public Result getIndexImg() {
        return success(ruleService.getById(17).getRuleValue());
    }
}

